package com.djx.comm.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 验证帮助类
 * @author dingjunxiong
 */
public class MatchesUtils {
    private MatchesUtils() {
        throw new IllegalStateException("MatchesUtils class");
    }

    public static final String MATCHES_W = "\\w+";
    public static final String MATCHES_EMAIL = "^([a-z0-9A-Z]+[-_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
    public static final String MATCHES_XLS = "^.+\\.(?i)(xls)$";

    /**
     * 验证 xlsx和xls格式的Excel
     * @param str
     * @return true：xlsx / false：xls
     */
    public static Boolean verifyExcel(String str) {
        Boolean bool = Boolean.TRUE;
        if (StringUtils.isNotBlank(str) && str.matches(MATCHES_XLS)) {
            bool = Boolean.FALSE;
        }
        return bool;
    }

    /**
     * 验证字符串是否是数字
     * @param str
     * @return
     */
    public static Boolean isNum(String str) {
        Boolean bool = Boolean.TRUE;
        for (int i = 0; i < str.length(); i++) {
            if (!Character.isDigit(str.charAt(i))) {
                bool = Boolean.FALSE;
            }
        }
        return bool;
    }

    /**
     * 验证 字符串 只有英文、数字、下划线
     * @param str
     * @return
     */
    public static Boolean verifyW(String str) {
        Boolean bool = Boolean.FALSE;
        if (StringUtils.isNotBlank(str) && str.matches(MATCHES_W)) {
            bool = Boolean.TRUE;
        }
        return bool;
    }

    /**
     * 邮箱验证
     * @param email
     * @return
     */
    public static Boolean verifyMail(String email) {
        Boolean bool = Boolean.FALSE;
        if (StringUtils.isNotBlank(email)) {
            Pattern p = Pattern.compile(MATCHES_EMAIL);
            Matcher m = p.matcher(email);
            if (m.matches()) {
                bool = Boolean.TRUE;
            }
        }
        return bool;
    }
}
